package io.imp.modules.tasks.unionqrc.service;

import io.imp.modules.sys.service.SysDictService;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayReverseJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * 功能描述:
 * <冲正文件数据处理>
 *
 * @ClassName: WorkItcsUnionpayReverseJnl
 * @Date: 2019年05月17日 13:31:04
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class WorkItcsUnionpayReverseJnl implements Callable {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private ItcsUnionpayReverseJnlService itcsUnionpayReverseJnlService;

    private ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService;

    private SysDictService sysDictService;

    private ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService;

    private List<ItcsUnionpayReverseJnl> source;

    private CountDownLatch countDownLatch;

    public WorkItcsUnionpayReverseJnl(ItcsUnionpayReverseJnlService itcsUnionpayReverseJnlService,
                                      ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService,
                                      SysDictService sysDictService, ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService,
                                      List<ItcsUnionpayReverseJnl> source, CountDownLatch countDownLatch){
        this.itcsUnionpayReverseJnlService = itcsUnionpayReverseJnlService;
        this.itcsUnionpayTxnJnlPartService = itcsUnionpayTxnJnlPartService;
        this.sysDictService = sysDictService;
        this.itcsUnionpayTxnJnlService = itcsUnionpayTxnJnlService;
        this.source = source;
        this.countDownLatch = countDownLatch;
    }
    @Override
    public Object call() throws Exception {
        logger.info("冲正流水数据处理...");
        StringBuilder result = new StringBuilder();

        if (source.size()!=0){
            for (ItcsUnionpayReverseJnl i: source) {

                //获取原始渠道订单实体
                String origqryid = i.getOrigqryid();
                ItcsUnionpayTxnJnl itcsUnionpayTxnJnl = itcsUnionpayTxnJnlService.
                        getItcsUnionpayTxnJnlWithOne(new String[] { "queryid" }, new Object[] {origqryid});

                String txntype = i.getTxntype();
                txntype = StringUtils.isBlank(txntype)?"99":txntype;
                String txnsubtype = i.getTxnsubtype();
                txnsubtype = StringUtils.isBlank(txnsubtype)?"01":txnsubtype;
                result.append(i.getMerid()).append("|").append(i.getTermid()).append("|")
                        .append("").append("|").append("")
                        .append("|").append(i.getTxntime()).append("|")
                        .append("").append("|").append(i.getTxnamt())
                        .append("|").append(txntype).append("|")
                        .append(txnsubtype).append("|").append("")
                        .append("|").append(i.getRespcode()).append("|").append(i.getRespmsg())
                        .append("|").append("").append("|")
                        .append("").append("|").append(itcsUnionpayTxnJnl.getAccno())
                        .append("|").append(itcsUnionpayTxnJnl.getOrderid()).append("|").append("冲正").append("\r\n");
                itcsUnionpayReverseJnlService.update("1",i.getTxnno());
            }
        }else {
            result.append("");
        }
        this.countDownLatch.countDown();
        return result.toString().replace("null","");
    }
}
